from flask import Flask
from app.config import Config
from app.utils.import_utils import import_models_recursively

# 从model.py中获取单例的db对象（确保导入路径正确）
from app.models.model import get_db


def create_app(db):
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = Config.SQLALCHEMY_DATABASE_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)
    # 自动导入models包及其子包下所有模型类
    import_models_recursively('app.models')
    return app


if __name__ == '__main__':
    db = get_db()
    app = create_app(db)
    with app.app_context():
        # db.drop_all()
        db.create_all()
